<?php
require("simple_html_dom.php");
include "config.php";
Requests::register_autoloader();
header('Content-type: application/json');

$r = array(
    "title" => "",
    "time" => "",
    "wordage" => null,
    "tag" => "",
    "content" => null
);

if (!isset($_GET["id"])) {
    echo json_encode($r);
    die(0);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.jianshu.com/p/".$_GET["id"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$response = curl_exec($ch);

if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 404){
    echo json_encode($r);
    die(0);
}
curl_close($ch);

$html = new simple_html_dom();
$html->load($response);
$data = json_decode($html->find("script[data-name=page-data]")[0]->innertext, true);
$r["title"] = $html->find("h1.title")[0]->plaintext;
$r["content"] = $html->find("div.show-content")[0]->innertext;
$r["time"] = $html->find("span.publish-time")[0]->plaintext;
$r["wordage"] = $data["note"]["public_wordage"];
$r["tag"] = $html->find("a.notebook span")[0]->plaintext;

echo json_encode($r);

?>